<?php
session_start();

$_SESSION['team1History'] = 0;
$_SESSION['team2History'] = 0;

include('dbcon.php');
$querystring = "";
$querystring2 = "";
$querystring3 = "";
$querystring4 = "";
$querystring5 = "";
$team1Totalresult = 0;
$team1Homeresult = 0;
$team2Totalresult = 0;
$totalhomegames = 0;
$home =  "";
$away = "";
if(isset($_POST['home']) && isset($_POST['away'])) {
     $querystring = "select * from history where (home='".$_POST['home']."' and away='".$_POST['away']."') or (home='".$_POST['away']."' and away='".$_POST['home']."')";
     $querystring2 = "select count(*), t.name from history h, lut_teams t where ((h.home='".$_POST['home']."' and h.away='".$_POST['away']."' and h.homescore>h.awayscore) or (h.away='".$_POST['home']."' and h.home='".$_POST['away']."' and h.homescore<h.awayscore)) and t.code='".$_POST['home']."'";
     $querystring3 = "select count(*), t.name from history h, lut_teams t where ((h.home='".$_POST['away']."' and h.away='".$_POST['home']."' and h.homescore>h.awayscore) or (h.away='".$_POST['away']."' and h.home='".$_POST['home']."' and h.homescore<h.awayscore)) and t.code='".$_POST['away']."'";
    $querystring4 = "select count(*) from history h where (h.home='".$_POST['home']."' and h.away='".$_POST['away']."' and h.homescore>h.awayscore)";
    $querystring5 = "select count(*) from history h where h.home='".$_POST['home']."' and h.away='".$_POST['away']."'";
}


#RAW HISTORY DATA
if ($stats = $mysqli->query($querystring)) {


while($statrow = $stats->fetch_array())
{
$statrows[] = $statrow;
}

foreach($statrows as $row)
{
?>
<tr class="result">
    <td><?php echo $row['date']; ?></td>
    <td><?php echo $row['kickoff']; ?></td>
    <td><?php echo $row['home']; ?></td>
    <td><?php echo $row['away']; ?></td>
    <td><?php echo $row['homescore']; ?></td>
    <td><?php echo $row['awayscore']; ?></td>
    <td><?php echo $row['homeodds']; ?></td>
    <td><?php echo $row['drawodds']; ?></td>
    <td><?php echo $row['awayodds']; ?></td>
</tr>
<?php
}

/* free result set */
    $stats->close();
}

#RESULTS
if ($teamscore1 = $mysqli->query($querystring2)) {

    $result1 = $teamscore1->fetch_row();
    $team1Totalresult = $result1[0];
    $home = $result1[1];
    $teamscore1->close();
}
if ($teamscore2 = $mysqli->query($querystring3)) {

    $result2 = $teamscore2->fetch_row();
    $team2Totalresult = $result2[0];
    $away = $result2[1];
    $teamscore2->close();

}
if ($teamscore3 = $mysqli->query($querystring4)) {

    $result3 = $teamscore3->fetch_row();
    $team1Homeresult = $result3[0];
    $teamscore3->close();
}
if ($teamscore4 = $mysqli->query($querystring5)) {

    $result4 = $teamscore4->fetch_row();
    $totalhomegames = $result4[0];
    $teamscore4->close();

}

$totalgames = $team1Totalresult + $team2Totalresult;
$hometotalratio = $team1Totalresult/$totalgames;
$homewinratio = $team1Homeresult/$totalhomegames;
$homeratio = round(($homewinratio*0.8) + ($hometotalratio*0.2), 2);
$awayratio = 1 - $homeratio;

?>
<tr class="result">
    <td colspan="9"><?php echo $home." won ".$team1Totalresult." game(s)."; ?></td>
</tr>
<tr class="result">
    <td colspan="9"><?php echo $away." won ".$team2Totalresult." game(s)."; ?></td>
</tr>

<tr class="result">
    <td colspan="9"><?php echo $home." have an historical ratio of ".$homeratio." game(s)."; ?></td>
</tr>
<tr class="result">
    <td colspan="9"><?php echo $away." have an historical ratio of ".$awayratio." game(s)."; ?></td>
</tr>